//
//  NSArray+Crash.m
//  flutter
//
//  Created by GilesZhou on 2020/6/28.
//  Copyright © 2020 Ganee. All rights reserved.
//

#import "NSArray+Crash.h"
#import "CrashManager.h"

@implementation NSArray (Crash)

+ (void)ch_observeCrash {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        // 类方法
//        [CrashManager exchangeClassMethod:[self class] method1Sel:@selector(arrayWithObjects:count:) method2Sel:@selector(ch_arrayWithObjects:count:)];
        
        
        // 实例方法
        Class __NSArray = NSClassFromString(@"NSArray");
        Class __NSArrayI = NSClassFromString(@"__NSArrayI");
        Class __NSSingleObjectArrayI = NSClassFromString(@"__NSSingleObjectArrayI");
        Class __NSArray0 = NSClassFromString(@"__NSArray0");
        Class __NSPlaceholderArray = NSClassFromString(@"__NSPlaceholderArray");
        
        [CrashManager exchangeInstanceMethod:__NSPlaceholderArray method1Sel:@selector(initWithObjects:count:) method2Sel:@selector(ch_initWithObjects:count:)];
        [CrashManager exchangeInstanceMethod:__NSArray method1Sel:@selector(objectsAtIndexes:) method2Sel:@selector(ch_objectsAtIndexes:)];
        [CrashManager exchangeInstanceMethod:__NSArrayI method1Sel:@selector(objectAtIndex:) method2Sel:@selector(ch_NSArrayIAtIndex:)];
        [CrashManager exchangeInstanceMethod:__NSSingleObjectArrayI method1Sel:@selector(objectAtIndex:) method2Sel:@selector(ch_NSSingleObjectArrayIAtIndex:)];
        [CrashManager exchangeInstanceMethod:__NSArray0 method1Sel:@selector(objectAtIndex:) method2Sel:@selector(ch_NSArray0AtIndex:)];
    });
    
}

- (id)ch_NSArrayIAtIndex:(NSUInteger)index {
    id result = nil;
    
    @try {
        result = [self ch_NSArrayIAtIndex:index];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return result;
    }
}

- (id)ch_NSSingleObjectArrayIAtIndex:(NSUInteger)index {
    id result = nil;
    
    @try {
        result = [self ch_NSSingleObjectArrayIAtIndex:index];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return result;
    }
}

- (id)ch_NSArray0AtIndex:(NSUInteger)index {
    id result = nil;
    
    @try {
        result = [self ch_NSArray0AtIndex:index];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return result;
    }
}

- (NSArray *)ch_objectsAtIndexes:(NSIndexSet *)indexes {
    NSArray *result = nil;
    
    @try {
        result = [self ch_objectsAtIndexes:indexes];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return result;
    }
}

+ (instancetype)ch_arrayWithObjects:(const id  _Nonnull __unsafe_unretained *)objects count:(NSUInteger)cnt {
    id instance = nil;
    
    @try {
        instance = [self ch_arrayWithObjects:objects count:cnt];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return instance;
    }
}

- (instancetype)ch_initWithObjects:(const id _Nonnull [_Nullable])objects count:(NSUInteger)cnt {
    id instance = nil;
    
    @try {
        instance = [self ch_initWithObjects:objects count:cnt];
    } @catch (NSException *exception) {
        UncaughtExceptionHandler(exception);
    } @finally {
        return instance;
    }
}

@end
